/*
 * Copyright (c) zhg2yqq.com Corp.
 * All Rights Reserved.
 */
package com.zhg2yqq.wheels.job.execution;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhg2yqq.wheels.job.constant.JobConstant;
import com.zhg2yqq.wheels.job.core.JobFinishEvent;
import com.zhg2yqq.wheels.job.core.JobParameter;
import com.zhg2yqq.wheels.job.core.JobStartEvent;

import cn.hutool.extra.spring.SpringUtil;

/**
 * @author zhg2yqq, 2023年2月17日
 * @version zhg2yqq v1.0
 */
public abstract class AbstractQuartzJob implements Job {
    private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobParameter scheduleParameter = (JobParameter) context.getMergedJobDataMap().get(JobConstant.TASK_PROPERTIES);

        JobFinishEvent finishEvent = new JobFinishEvent(scheduleParameter);
        if (scheduleParameter != null) {
            finishEvent.setStartTime(new Date());
            try {
                SpringUtil.publishEvent(new JobStartEvent(scheduleParameter));
                doExecute(context, scheduleParameter);
            } catch (Exception e) {
                log.error("任务执行异常  - ：", e);
                finishEvent.setEx(e);
            } finally {
                finishEvent.setEndTime(new Date());
            }
            SpringUtil.publishEvent(finishEvent);
        }
    }

//    /**
//     * 执行前
//     *
//     * @param context 工作执行上下文对象
//     * @param sysJob 系统计划任务
//     */
//    protected void before(JobExecutionContext context, SysJob sysJob) {
//        threadLocal.set(new Date());
//    }
//
//    /**
//     * 执行后
//     *
//     * @param context 工作执行上下文对象
//     * @param sysJob 系统计划任务
//     */
//    protected void after(JobExecutionContext context, SysJob sysJob, Exception e) {
//        try {
//            Date startTime = threadLocal.get();
//            final SysJobLog sysJobLog = new SysJobLog();
//            sysJobLog.setJobName(sysJob.getJobName());
//            sysJobLog.setJobGroup(sysJob.getJobGroup());
//            sysJobLog.setInvokeTarget(sysJob.getInvokeTarget());
//            sysJobLog.setStartTime(startTime);
//            sysJobLog.setEndTime(new Date());
//            long runMs = sysJobLog.getEndTime().getTime() - sysJobLog.getStartTime().getTime();
//            sysJobLog.setJobMessage(sysJobLog.getJobName() + " 总共耗时：" + runMs + "毫秒");
//            if (e != null) {
//                sysJobLog.setStatus(ScheduleConstant.JobStatus.FAIL.getValue());
//                String errorMsg = ExceptionUtil.stacktraceToString(e, 2000);
//                sysJobLog.setExceptionInfo(errorMsg);
//            } else {
//                sysJobLog.setStatus(ScheduleConstant.JobStatus.SUCCESS.getValue());
//            }
//            // 写入数据库当中
//            SpringUtil.getBean(ISysJobLogService.class).save(sysJobLog);
//        } finally {
//            threadLocal.remove();
//        }
//    }

    /**
     * 执行方法，由子类重载
     *
     * @param context   工作执行上下文对象
     * @param parameter 系统计划任务
     * @throws Exception 执行过程中的异常
     */
    protected abstract void doExecute(JobExecutionContext context, JobParameter parameter) throws Exception;
}
